package bbb.sss;

import java.util.List;
import java.util.Map.Entry;

import bbb.BallInfo;
import bbb.BallMap;

public class RedCountStatistic {

	private StatisticObject so;

	public RedCountStatistic(StatisticObject so) {
		super();
		this.so = so;
	}

	public void redCount(List<BallInfo> data, String code) {
		so.setCode(code);
		if (data != null && data.size() > 0) {
			int count = 0;
			for (int i = 0; i < data.size(); i++) {
				BallInfo bi = data.get(i);
				if (bi != null && bi.getCode().compareTo(code) <= 0) {
					addRed(bi.getRed1());
					addRed(bi.getRed2());
					addRed(bi.getRed3());
					addRed(bi.getRed4());
					addRed(bi.getRed5());
					addRed(bi.getRed6());

					count++;
				}
			}
			so.setHistoryCount(count);
		}
	}

	public String printBallRedCount() {
		List<Entry<Integer, Integer>> rr = so.getRedCount().keySortedList();
		StringBuffer sb = new StringBuffer();
		sb.append(so.getCode() + " ");
		for (Entry<Integer, Integer> e :rr) {
			sb.append(Utils.format("%5d", e.getValue()));
		}
		return sb.toString();
	}
	
	public String printBallRedRate() {
		List<Entry<Integer, Integer>> rr = so.getRedCount().keySortedList();
		StringBuffer sb = new StringBuffer();
		sb.append(so.getCode() + " ");
		for (Entry<Integer, Integer> e : rr) {
			sb.append(Utils.format("%5d", e.getValue() * 550 / so.getHistoryCount() ));
		}
		return sb.toString();
	}

	public String printSortRedCount() {
		List<Entry<Integer, Integer>> rr = so.getRedCount().valueSortedList();
		StringBuffer sb = new StringBuffer();
		StringBuffer sb1 = new StringBuffer();
		sb.append(Utils.format("%7d", (so.getHistoryCount() * 6 / 33)));
		sb1.append(so.getCode() + " ");
		for (int i = 0; i < rr.size(); i++) {
			Entry<Integer, Integer> e = rr.get(i);
			sb.append(Utils.format("%5d", e.getKey()));
			sb1.append(Utils.format("%5d", e.getValue()));
		}
		return sb.append(sb1).toString();
	}

	private void addRed(Integer red) {
		CountMap mm = so.getRedCount();
		Integer rr = mm.get(red);
		if (rr == null) {
			mm.put(red, 1);
		} else {
			mm.put(red, rr + 1);
		}
	}

	public static void main(String[] args) {
		BallMap.loadData("C:\\Users\\Administrator\\Desktop\\双色球\\r.txt");
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		StringBuffer sb3 = new StringBuffer();
		for(int i = 0 ; i < BallMap.data.size() ; i++){
			String code = BallMap.data.get(i).getCode();
			StatisticObject so = new StatisticObject();
			RedCountStatistic rs = new RedCountStatistic(so);
			rs.redCount(BallMap.data, code);
			sb1.append(rs.printBallRedCount()).append("\r\n");
			sb2.append(rs.printBallRedRate()).append("\r\n");
			sb3.append(rs.printSortRedCount()).append("\r\n");
		}
		
		System.out.println(Utils.printHeader());
		System.out.println(sb1.toString());
		System.out.println("=============================================================================================================");
		System.out.println(Utils.printHeader());
		System.out.println(sb2.toString());
		System.out.println("=============================================================================================================");
		System.out.println(Utils.printHeader());
	}
}
